package com.lcc.forum.demos.web.controller;

import com.lcc.forum.demos.web.common.AppResult;
import com.lcc.forum.demos.web.common.ResultCode;
import com.lcc.forum.demos.web.exception.ApplicationException;
import com.lcc.forum.demos.web.model.Board;
import com.lcc.forum.demos.web.services.IBoradService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 版块接口
 */
@RestController
@Api(tags = "版块接口")
@Slf4j
@RequestMapping("/board")
public class BoradController {
    //注入配置的版块数量,如果没有配置，默认值为9
    @Value("${bit-forum.index.board-num:9}")
    private Integer indexBoardNum;
    //注入service

    @Resource
    private IBoradService boradService;
    @ApiOperation("/获取首页版块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        //调用service查询结果
        List<Board> boards = boradService.selectByNum(indexBoardNum);
        //判断是否为空
        if (boards == null){
            boards = new ArrayList<>();
        }
        //返回结果
        return AppResult.success(boards);
    }
    @ApiOperation("获取版块信息")
    @GetMapping("/getById")
    public AppResult<Board> getById(@ApiParam("版块id") @RequestParam("id") @NonNull Long id){
        //参数校验
        //查询
        Board board = boradService.selectById(id);
        //校验版块是否存在
        if (board == null || board.getDeleteState() == 1){//表示版块记录在数据库中不存在或者是已删除状态
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //返回结果
        return AppResult.success(board);
    }
}
